#include <iostream>
#include <cassert>

using namespace std;

namespace YX
{
    

    template<class T>
    class vector 
    {   
        public:
            typedef T* Iterator;
            typedef const T* Iterator_const;


            vector()
                :_start(nullptr)
                ,_finish(nullptr)
                ,_end(nullptr)
            {

            }

            vector(size_t n,const T& val=T())
                :_start(nullptr)
                ,_finish(nullptr)
                ,_end(nullptr)
            {
                reserve(n);
                for(size_t i=0;i<n;i++)
                {
                    _start[i]=val;
                }
                _finish=_start+n;
            }

            template<class InputIterator>
            vector(InputIterator* left,InputIterator* right)
                :_start(nullptr)
                ,_finish(nullptr)
                ,_end(nullptr) 
            {
                while(left!=right)
                {
                    push_back(*left);
                    left++;
                }
            }

            vector(const vector<T>& v)
                :_start(nullptr)
                ,_finish(nullptr)
                ,_end(nullptr)
            {
                
                vector<T> tmp(v.begin(),v.end());

                swap(tmp);

            }

            size_t capacity()
            {
                return _end-_start;
            }
            
            size_t size()
            {
                return _finish-_start;
            }

            void reserve(size_t n)
            {
                if(n>capacity())
                {
                    size_t sz=size();

                    Iterator tmp=new T[n];
                    
                    if(_start!=nullptr)
                    {
                        for(size_t i=0;i<sz;i++)
                        {
                            tmp[i]=_start[i];
                        }
                        delete[] _start;
                    }

                    _start=tmp;
                    _finish=_start+sz;
                    _end=_start+n;
                }
            }

            Iterator begin()
            {
                return _start;
            }

            Iterator end()
            {
                return _finish;
            }

            void swap(const vector<T>& v)
            {
                ::swap(_start,v._start);
                ::swap(_finish,v._finish);
                ::swap(_end,v._end);
            }

            void push_back(const T& val)
            {
                if(_finish==_end)
                {
                    reserve(capacity()==0?4:2*capacity());
                }

               (*_finish)=val;

                _finish++;
            }
            // 插入
           Iterator insert(Iterator pos,const T& val)
           {
               assert(pos>=_start&&pos<=_finish);
               if(_finish==_end)
               {
                   size_t posSize=pos-_start;
                   reserve(capacity()==0?4:2*capacity());
                   pos=_start+posSize;
               }

               Iterator ptr=_finish-1;

               while(ptr>=pos)
               {
                    *(ptr+1)=*ptr;
                    --ptr;
               }
            
               *pos=val;

               _finish++;

               return pos;
           }
            // 删除
            Iterator erase(Iterator pos)
            { 
               assert(pos>=_start&&pos<_finish);
                
               Iterator ptr=pos+1;
               while(ptr<=_finish)
               {
                   *(ptr-1)=*ptr;
                   ++ptr;
               }

               --_finish;
               return pos;
            }

        private:
            Iterator _start;
            Iterator _finish;
            Iterator _end;
    };



    void TestVector1()
    {
//        vector<int> v;
//
//        v.push_back(1);
//        v.push_back(1);
//        v.push_back(1);
//        v.push_back(1);
//        v.push_back(1);
//        v.push_back(1);

        vector<int> v(10,1);
        
        v.insert(v.end(),0);

        for(const auto& e:v)
        {
            cout<<e<<" ";
        }
        cout<<endl;
    }

    void TestVector2()
    {
        vector<int> v;

        v.push_back(1);
        v.push_back(2);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
        v.push_back(6);
        v.push_back(7);
        v.push_back(8);

        vector<int>::Iterator it=v.begin();

        while(it!=v.end())
        {
            if((*it)%2==0)
            {
                it=v.erase(it);
            }
            else 
            {
                it++;
            }
        }

        for(const auto& e:v)
        {
            cout<<e<<" ";
        }
        cout<<endl;
    }

};
